package ch.bfh.abcmmh.controller.operating.scanner;

import ch.bfh.abcmmh.controller.operating.interfaces.IStreetScanner;
import ch.bfh.abcmmh.model.streetutils.Street;

public class StreetScanner implements IStreetScanner {

	protected Street street;

	public StreetScanner(Street street) {
		this.street = street;
	}

	@Override
	public int getDistObstacleAhead(int actualPosition) {
		int freePositions = 0;
		if (street.getHighestIndex() != actualPosition) {
			while (street.getSAttribute(++actualPosition).isFree()) {

				freePositions++;
				
				//workaround
				if (freePositions >= 200) {
					return 200;
				}

				if (street.getHighestIndex() == actualPosition) {
					// workaround
					return 200;
				}
			}
		} else if (street.getHighestIndex() == actualPosition) {
			// workaround
			return 200;
		}
		return freePositions;
	}

	@Override
	public int getDistObstacleBehind(int actualPosition) {
		int serch = actualPosition;
		if (!street.getSAttribute(serch).isFree()) {
			return 0;
		}
		if (serch-1<=0) {
			return 0;
		}
		if (!street.getSAttribute(serch - 1).isFree()) {
			return 0;
		}

		 while (street.getSAttribute(serch).isFree() && serch != 0){
			 serch--;
		 }

		 if (serch == 0) {
			serch--;
		}
		return actualPosition - (serch+1);
	}

}
